<template>
	<view class="container">
		<!-- 收藏列表 -->
		<scroll-view 
			scroll-y 
			class="favorite-scroll"
			@scrolltolower="loadMore"
			refresher-enabled
			:refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh"
		>
			<view class="favorite-list">
				<view 
					class="favorite-item" 
					v-for="(item, index) in favoriteList" 
					:key="index"
				>
					<image :src="item.image" mode="aspectFill" class="product-image" @click="navigateToDetail(item.id)"></image>
					<view class="product-info">
						<text class="name" @click="navigateToDetail(item.id)">{{item.name}}</text>
						<text class="price">¥{{item.price}}</text>
					</view>
					<view class="action-btn" @click="cancelFavorite(item.id)">
						<text class="icon">❌</text>
					</view>
				</view>
			</view>
			
			<!-- 加载更多 -->
			<view class="loading-more" v-if="hasMore">
				<text>加载中...</text>
			</view>
			<view class="no-more" v-else>
				<text>没有更多了</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="favoriteList.length === 0">
				<image src="/static/images/empty.png" mode="aspectFit" class="empty-img"></image>
				<text class="empty-text">暂无收藏商品</text>
				<view class="go-shopping" @click="goShopping">
					<text>去逛逛</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
import { ref } from 'vue'

// 是否还有更多数据
const hasMore = ref(true)

// 是否正在刷新
const isRefreshing = ref(false)

// 收藏列表
const favoriteList = ref([
	{
		id: 1,
		name: '川味火锅底料',
		price: '39.90',
		image: 'https://img.alicdn.com/imgextra/i4/O1CN01XCiY1B1nx2Xh2Z9tP_!!6000000005155-0-tps-1316-736.jpg'
	},
	{
		id: 2,
		name: '麻辣牛肉',
		price: '68.00',
		image: 'https://img.alicdn.com/imgextra/i2/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
	},
	{
		id: 3,
		name: '毛肚',
		price: '88.00',
		image: 'https://img.alicdn.com/imgextra/i3/O1CN01F0MA5j1zXb0XcYV1F_!!6000000006733-0-tps-1316-736.jpg'
	}
])

// 加载更多
const loadMore = () => {
	if (!hasMore.value) return
	
	// 这里可以添加加载更多收藏商品的接口调用
	setTimeout(() => {
		hasMore.value = false
	}, 1000)
}

// 下拉刷新
const onRefresh = () => {
	isRefreshing.value = true
	// 重置列表并重新加载
	favoriteList.value = []
	hasMore.value = true
	
	setTimeout(() => {
		isRefreshing.value = false
		loadMore()
	}, 1000)
}

// 跳转到商品详情
const navigateToDetail = (id) => {
	uni.navigateTo({
		url: `/pages/detail/view?id=${id}`
	})
}

// 取消收藏
const cancelFavorite = (id) => {
	uni.showModal({
		title: '提示',
		content: '确定取消收藏该商品吗？',
		success: (res) => {
			if (res.confirm) {
				// 这里可以添加取消收藏的接口调用
				uni.showToast({
					title: '已取消收藏',
					icon: 'success'
				})
				// 从列表中移除
				favoriteList.value = favoriteList.value.filter(item => item.id !== id)
			}
		}
	})
}

// 去逛逛
const goShopping = () => {
	uni.switchTab({
		url: '/pages/index/index'
	})
}
</script>

<style>
.container {
	min-height: 100vh;
	background-color: #F5F5F5;
}

/* 收藏列表样式 */
.favorite-scroll {
	height: 100vh;
}

.favorite-list {
	padding: 20rpx;
}

.favorite-item {
	display: flex;
	align-items: center;
	background-color: #FFFFFF;
	padding: 20rpx;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
}

.product-image {
	width: 160rpx;
	height: 160rpx;
	border-radius: 10rpx;
	margin-right: 20rpx;
}

.product-info {
	flex: 1;
}

.name {
	font-size: 28rpx;
	color: #333;
	margin-bottom: 20rpx;
	display: block;
}

.price {
	font-size: 32rpx;
	color: #D02B29;
	font-weight: bold;
}

.action-btn {
	padding: 20rpx;
}

.action-btn .icon {
	font-size: 40rpx;
}

/* 加载更多样式 */
.loading-more, .no-more {
	text-align: center;
	padding: 20rpx 0;
	font-size: 24rpx;
	color: #999;
}

/* 空状态样式 */
.empty-state {
	padding: 100rpx 0;
	text-align: center;
}

.empty-img {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
	margin-bottom: 40rpx;
	display: block;
}

.go-shopping {
	display: inline-block;
	padding: 20rpx 60rpx;
	background-color: #D02B29;
	color: #FFFFFF;
	font-size: 28rpx;
	border-radius: 40rpx;
}
</style> 